package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.common.SetmealDishMapper;
import com.sky.mapper.common.DishFlavorMapper;
import com.sky.mapper.common.DishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description DishServiceImpl
 * @Author 王富贵
 * @Date 2024-10-06
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public Result<String> sava(DishDTO dto) {


        //1.创建dish对象
        Dish dish = new Dish();
        //2.dto属性拷贝到dto
        BeanUtils.copyProperties(dto, dish);
        //3.新增dish入库
        dishMapper.sava(dish);
        //4.给dto的flavor对象设置dishid
        dto.getFlavors().forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        //5.批量新增入库即可
        dishFlavorMapper.batchSava(dto.getFlavors());
        return Result.success();
    }

    @Override
    public Result<PageResult> page(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<DishVO> page = dishMapper.page(dto);
        //结果封装
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return Result.success(pageResult);

    }

    /**
     * 菜品批量删除
     *
     * @param ids
     */

    @Override
    @Transactional
    public Result<String> deleteByIds(List<Long> ids) {
        //查询删除的菜品是否有在起售中的
        Integer count = dishMapper.selectCountByIdsAndStatus(ids, StatusConstant.ENABLE);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //查询删除的菜品是否有被套餐关联的
        Integer count2 = setmealDishMapper.selectCountByDishIds(ids);
        if (count2 > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //都没有，删除菜品的口味信息
        dishFlavorMapper.deleteByDishIds(ids);

        //删除菜品信息
        dishMapper.deleteByIds(ids);
        return Result.success();
    }

    @Override
    public Result<DishVO> selectDishWithFlavor(Long dishId) {
        //多表查询
        DishVO dishVO = dishMapper.selectDishWithFlavor(dishId);
        return Result.success(dishVO);
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //        1.修改dish表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
//        2.修改dish_flavor表(对于关系表的修改操作)
//        a.先删除原有数据
        dishFlavorMapper.deleteByDishId(dish.getId());
//        b.添加新的数据 -- 批量新增
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishFlavorMapper.batchSave(flavors);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 根据套餐id查询菜品
     */
    @Override
    public List<Dish> getBySetmealId(Long categoryId) {
        return dishMapper.getBySetmealId(categoryId);

    }

    /**
     * 条件查询菜品和口味
     *
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
